﻿using System;
using System.Configuration;

namespace BizControl.Dal
{
  public static class DalFactory
  {
    private static Type _dalType;
    private static readonly string DalManagerType = @"DalManagerType";

    public static IDalManager GetManager()
    {
      if (_dalType == null)
      {
        var dalTypeName = ConfigurationManager.AppSettings[DalManagerType];
        if (!string.IsNullOrEmpty(dalTypeName))
          _dalType = Type.GetType(dalTypeName);
        else
          throw new NullReferenceException(Properties.Resources.DalManagerNameIsNullOrEmpty);
        if (_dalType == null)
          throw new ArgumentException(string.Format(Properties.Resources.DalManagerTypeNotFound, dalTypeName));
      }
      return (IDalManager)Activator.CreateInstance(_dalType);
    }
  }
}
